/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.engine.task;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.seari.bean.PageFetcher;
import com.seari.engine.RollPageSpider;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Rainbow
 */
public class SinglePageTask implements Runnable
{
    protected BlockingQueue<PageFetcher> queue;
    protected WebClient webClient;

    public SinglePageTask(BlockingQueue<PageFetcher> queue)
    {
        this.queue = queue;
    }
    
    
    @Override
    public void run()
    {
        spider();
    }
    
    public void spider()
    {
        while (true)
        {            
            try
            {
                PageFetcher pageFetcher;
                pageFetcher = queue.poll(3, TimeUnit.SECONDS);
                if(pageFetcher == null)
                {
                    continue;
                }
                //init();
                RollPageSpider spider = new RollPageSpider(pageFetcher.subSite);

                
            } catch (Exception e)
            {
            }
            finally{
                if(webClient != null)
                {
                    webClient.close();
                    webClient = null;
                }
            }
        }
    }
    
    private void init()
    {
        webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setTimeout(10000);
        webClient.setJavaScriptTimeout(5000);
    }
}
